<?php

/* 
 * mysqli
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class db_mysqli
{
    private $username = null;
    private $password = null;
    private $hostname = null;
    private $database = null;
    private $port = null;
    private $conn = null;
    private $table_ext = null;
    private $start_time = null;
    private $end_time = null;
    private $charset = null;
    private $sql = null;
    private static $instance = null;
    protected $error_info = '';
    /**
     * 
     * @param type $username
     * @param type $password
     * @param type $hostname
     * @param type $database
     * @param type $table_ext
     * @param type $port
     * @param type $charset
     */
    public function __construct($username, $password, $hostname, $database, $table_ext, $port, $charset) {                
        $this->username = $username;
        $this->password = $password;
        $this->hostname = $hostname;
        $this->database = $database;
        $this->table_ext = $table_ext;
        $this->port = $port;
        $this->charset = $charset;
        $this->start_time = microtime(true);
        $this->pconnect();
    }
    /**
     * 表前缀
     * @param type $table
     * @return type
     */
    protected function table($table) {
        return $this->table_ext . $table;
    }
    /**
     * 获取当前执行的SQL
     * @return type
     */
    protected function getSql(){
        return $this->sql;
    }
    
    /**
     * 获取执行的时间
     * @return string
     */
    protected function getExeTiem() {
        $result = round($this->end_time - $this->start_time, 2) . 's';
        return $result;
    }
    /**
     * 数据库连接
     */
    private function pconnect() {
        try {
            $this->conn = mysqli_connect($this->hostname, $this->username, $this->password, $this->database, $this->port);
            if(!$this->conn || mysqli_connect_errno() > 0) { //0为连接成功 
                throw new Exception('连接失败' . mysqli_connect_errno());
            }
            if(strpos($this->charset, '-') !== false) {
                $this->charset = str_replace('-', '', $this->charset);
            }
            $this->query("set names {$this->charset}");
            $this->query("SET sql_mode=''");
            $this->query("SET character_set_connection=$this->charset, character_set_results=$this->charset, character_set_client=binary");
        } catch (Exception $ex) {
            $this->error_info = $ex->getMessage();
            mylogsql($this->error_info, '数据连接错误');
        }
    }
    /**
     * 获取连接的对象
     * @return object 对象
     */
    protected function getConn() {
        return $this->conn;
    }
    /**
     * 获取执行时间
     * @return type
     */
    public function exec_time() {
        return round($this->end_time - $this->start_time, 4);
    }

    /**
     * 获取插入的新ID
     * @return int ID
     */
    protected function insert_id() {
        return mysqli_insert_id($this->conn);
    }
    /**
     * 执行query
     * @param type $sql
     * @return type
     */
    protected function query($sql) {
        if(!$this->conn) {
            return false;
        }
        $query = mysqli_query($this->conn, $sql);
        if(!$query) {
            $msg = mysqli_error($this->conn);
            mylogsql(array('msg' => $msg, 'sql' => $sql), 'query error');
        }
        $this->sql = $sql;
        $this->end_time = microtime(TRUE);
        return $query;
    }
    /**
     * 返回行数
     * @param string $sql SQL语句
     * @return int 行数
     */
    protected function result_rows($sql) {
        if($query = $this->query($sql)) {
            $rows =  mysqli_num_rows($query);
            $this->free_conn($query);
        }
        return $rows;
    }
    /**
     * 返回结果集 二维
     * @param type $sql
     * @return type
     */
    protected function result_array($sql){
        $query = $this->query($sql);        
        if($query !== false) {
            $result = array();
            while ($row = mysqli_fetch_array($query, MYSQL_ASSOC)) {
                $result[] = $row;
            }
            return $result;
        } else {
            return false;
        }
       
    }
    protected function result_col($sql) {
        $query = $this->query($sql);
        if ($query !== false)
        {
            $result = array();
            while ($row = mysqli_fetch_row($query))
            {
                $result[] = $row[0];
            }  
            return $result;
        }
        else
        {
            return false;
        }
    }
    /**
     * 返回结果集,一维
     * @param type $sql
     * @return type
     */
    protected function result_row($sql) {
        $query = $this->query($sql);
        if($query !== false) {
            $result = mysqli_fetch_assoc($query);
            return $result;
        } else {
            return false;
        }        
    }
    /**
     * 获取一个数据的结果
     * @param string $sql SQL语句
     * @return boolean|string 结果
     */
    protected function result_one($sql){
        if(stripos($sql, 'limit') === false) {
            $sql .= ' Limit 1';
        }        
        $query = $this->query($sql);
        if ($query !== false){
            $row = mysqli_fetch_row($query);
            if ($row !== false){
                return $row[0];
            } else {
                return '';
            }
        } else {
            return false;
        }
    }    

    protected function trans_begin() {
        mysqli_autocommit($this->conn, false);
    }
    protected function trans_commit() {
        mysqli_commit($this->conn);
    }
    protected function trans_rollback() {
        mysqli_rollback($this->conn);
    }
    protected function free_conn($query) {
        mysqli_free_result($query);
    }
    protected function close_conn() {
        mysqli_close($this->conn);
    }
//    /**获取多少行*/
//    abstract protected function getCount($fileds, $where);
//    /**获取所有数据*/
//    abstract protected function getAll($fileds, $where);
//    /**获取一列数据*/
//    abstract protected function getCol($fileds, $where);   
//    /**获取一行数据*/
//    abstract protected function getRow($fileds, $where);
    
//    protected function __destruct() {
//        mysqli_close($this->conn);
//    }
}
